package com.longge;

public class ValidPalindrome {


    public boolean validPalindrome(String s) {
        boolean left = validPalindromeLeft(s);
        boolean right = validPalindromeRight(s);


        return left || right;
    }

    private boolean validPalindromeRight(String s) {
        char[] chars = s.toCharArray();
        boolean delFlag = false;

        int left = 0, right = chars.length - 1;

        while (left < right) {
            if (chars[left] != chars[right]) {
                if (!delFlag) {
                    if (chars[left + 1] == chars[right]) {
                        left++;
                    } else if (chars[left] == chars[right - 1]) {
                        right--;
                    } else {
                        break;
                    }
                    delFlag = true;
                } else {
                    break;
                }
            }
            left++;
            right--;
        }
        return left >= right;
    }

    private boolean validPalindromeLeft(String s) {
        char[] chars = s.toCharArray();
        boolean delFlag = false;

        int left = 0, right = chars.length - 1;

        while (left < right) {
            if (chars[left] != chars[right]) {
                if (!delFlag) {
                    if (chars[left] == chars[right - 1]) {
                        right--;
                    } else if (chars[left + 1] == chars[right]) {
                        left++;
                    } else {
                        break;
                    }
                    delFlag = true;
                } else {
                    break;
                }
            }
            left++;
            right--;
        }
        return left >= right;
    }




}
